class Lexer:
    def lexFile(self, filename):
        return self.lexString(open(filename,"r").read())
    
    def lexString(self, string):
        toks = []
        token = ""
        tokType = None
        startingToken = False
        parenCount = 0
        
        for char in string:
            if not tokType and not char.isspace():
                startingToken = True
                if char == "'":
                    tokType = 'literal'
                elif char == "\"":
                    tokType = 'string'
                elif char == "(":
                    tokType = 'compound'
                    parenCount = 1
                else:
                    tokType = 'simple'
                    
            if tokType == 'simple':
                if char.isspace():
                    if token.isdigit():
                        tokType = 'literal'
                        token = eval(token)
                    toks.append((token,tokType))
                    tokType = None
                else:
                    token += char
                    
            if not startingToken and tokType == 'literal':
                if char.isspace():
                    toks.append((token,tokType))
                    tokType = None
                else:
                    token += char
                    
            if not startingToken and tokType == 'string':
                if char == "\"":
                    toks.append((token,tokType))
                    tokType = None
                else:
                    token += char
                    
            if not startingToken and tokType == 'compound':
                token += char
                    
                if char == '(':
                    parenCount += 1
                elif char == ')':
                    parenCount -= 1
                    
                if not parenCount:
                    token = token[:-1]
                    toks.append((self.lexString(token),tokType))
                    tokType = None
                    
            startingToken = False
            if not tokType:
                token = ""
                
        if token and tokType:
            if tokType == 'simple' and token.isdigit():
                tokType = 'literal'
                token = eval(token)
            toks.append((token,tokType))
            
        return toks            
            
lexer = Lexer()
